﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using AuctionDataLayer;

namespace Auction_Intranet.Helpers
{
    /// <summary>
    /// The classes below take of loggin to both file and send email to administrator
    /// for exception that are throwned by the system.
    /// </summary>
    public class LogMedia
    {
        public virtual void LogMessage(string message)
        { 
            
        }
    }

    public class TextFileLogMedia : LogMedia
    {
        public string LogFilePath { get; set; }

        public TextFileLogMedia()
        {
            LogFilePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["LogFile"]);
        }
        public override void LogMessage(string message)
        {
            //log to database fix.
            AuctionDBDataContext m_db = new AuctionDBDataContext();            
            Log error = new Log();
            error.Date = DateTime.Now;
            error.Subject = "auction.ru.is Exception Thrown";
            error.Message = message;
            m_db.Logs.InsertOnSubmit(error);
            m_db.SubmitChanges();
            /***************************************/
            String strPath = Path.GetDirectoryName(LogFilePath);

            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }

            using (StreamWriter writer = new StreamWriter(LogFilePath, true, Encoding.Default))
            {
                writer.WriteLine(message);
            }
        }
    }

    public class EmailLogMedia : LogMedia
    {
        public override void LogMessage(string message)
        {
            try
            {
                string strEmail = ConfigurationManager.AppSettings["Email"];
                string strEmail2 = ConfigurationManager.AppSettings["Email2"];
                string srtSuject = ConfigurationManager.AppSettings["ExceptionSubject"];
                using (MailMessage the_message = new MailMessage())
                {
                    the_message.To.Add(strEmail);
                    the_message.Subject = srtSuject;
                    the_message.Body = message;
                    SmtpClient client = new SmtpClient();
                    client.EnableSsl = true;
                    client.Send(the_message);
                }
                using (MailMessage the_message = new MailMessage())
                {
                    the_message.To.Add(strEmail2);
                    the_message.Subject = srtSuject;
                    the_message.Body = message;
                    SmtpClient client = new SmtpClient();
                    client.EnableSsl = true;
                    client.Send(the_message);
                }
            }
            catch (Exception ex)
            {
                String LogFilePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["LogFile"]);
                String strPath = Path.GetDirectoryName(LogFilePath);
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }
                using (StreamWriter writer = new StreamWriter(LogFilePath, true, Encoding.Default))
                {
                    writer.WriteLine(string.Format("Could not send email notification.For: {0}{1}", ex.Source, ex.StackTrace));
                    if (ex.InnerException != null)
                    {
                        writer.WriteLine(string.Format("Could not send email notification.For: {0}{1}", ex.InnerException.Source, ex.InnerException.StackTrace));
                    }
                }
            }
        }
    }
}